package me.biezhi.longweibo.util;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HttpUtil {

	private HttpServletRequest request;
	
	public HttpUtil(HttpServletRequest request) {
		this.request = request;
	}
	
	public String getPara(String name){
		return request.getParameter(name);
	}
	
	public Integer getParaToInt(String name){
		if(null != this.getPara(name)){
			return Integer.valueOf(this.getPara(name));
		}
		return null;
	}
	
	public Long getParaToLong(String name){
		if(null != this.getPara(name)){
			return Long.valueOf(this.getPara(name));
		}
		return null;
	}
	
	public Boolean getParaToBoolean(String name){
		if(null != this.getPara(name)){
			return Boolean.valueOf(this.getPara(name));
		}
		return null;
	}
	
	public String getContextPath(){
		return request.getContextPath();
	}
	
	public String getBaseUrl(){
		return request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
	}
	
	public String getRootPath(){
		return request.getServletContext().getRealPath("/");
	}
	
	public void print(HttpServletResponse response, final String msg){
		try {
			response.setCharacterEncoding("utf-8");
			PrintWriter out = response.getWriter();
			out.print(msg);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
